package cn.ljn.pet.service.impl;

import cn.ljn.basic.util.BaiduAuditUtils;
import cn.ljn.basic.util.LoginContext;
import cn.ljn.org.domain.Employee;
import cn.ljn.org.domain.Shop;
import cn.ljn.org.mapper.EmployeeMapper;
import cn.ljn.pet.domain.Pet;
import cn.ljn.pet.domain.PetDetail;
import cn.ljn.pet.domain.PetOnlineAuditLog;
import cn.ljn.pet.mapper.PetDetailMapper;
import cn.ljn.pet.mapper.PetMapper;
import cn.ljn.pet.mapper.PetOnlineAuditLogMapper;
import cn.ljn.pet.service.IPetService;
import cn.ljn.basic.service.impl.BaseServiceImpl;
import cn.ljn.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljn
 * @since 2023-03-13
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper logMapper;

    @Autowired
    private EmployeeMapper  employeeMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        pet.setCreatetime(new Date());
        pet.setState(0);
//        Logininfo logininfo = LoginContext.getLogininfo();
//        Long id = logininfo.getId();
//     Employee employee =   employeeMapper.findByLogininfoId(id);
//        // 店铺id
//        pet.setShopId(employee.getShopId());
        super.add(pet);
        // 保存详情
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.updateByPetId(detail);
    }

    @Override
    public void onsale(List<Long> ids) {
        // 参数校验
        // 1.过滤已上架宠物
        List<Pet> petList = petMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(petList)) {
            return;
        }
        // 2.百度AI审核
        List<PetOnlineAuditLog> logList = new ArrayList<>();
        List<Long> petIdList = new ArrayList<>();
        petList.forEach(pet -> {
            StringBuilder sb = new StringBuilder();
            // 审核名称
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("名称审核不通过：").append(pet.getName());
            }
            // 审核resources
            String[] split = pet.getResources().split(",");
            Arrays.stream(split).forEach(resource -> {
                if (!BaiduAuditUtils.ImgCensor(resource)) {
                    sb.append("图片审核不通过：").append(pet.getResources());
                }
            });

            // 3.审核失败需要记录日志
            if (sb.length() > 0) {
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setPetId(pet.getId());
                auditLog.setState(0);
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setAuditId(-1L);
                logList.add(auditLog);
                return;
            }
            petIdList.add(pet.getId());
        });

        // 批量添加 审核日志
        if (!CollectionUtils.isEmpty(logList)) {
            logMapper.batchAdd(logList);
        }

        // 批量修改状态
        if (!CollectionUtils.isEmpty(petIdList)) {
            petMapper.batchUpdate(petIdList);


            // 根据ids分别查询出数据 如果不为空则进行添加
            if (petIdList != null) {
                for (Long petIds : petIdList){
                    Pet pet = petMapper.findById(petIds);
                    PetOnlineAuditLog log = new PetOnlineAuditLog();
                    log.setNote("审核通过：上架成功！");
                    log.setState(1);
                    log.setAuditId(-1L);
                    log.setPetId(pet.getId());
                    log.setAuditTime(new Date());
                    logMapper.add(log);
                }
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.batchUpdateOff(ids);
    }
}
